package com.example.amirani.myapplication;

import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;
import android.support.v4.app.RemoteInput;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MyActivity extends Activity {
    public static final String EXTRA_VOICE_REPLY = "extra_voice_reply"; //key for the voice input - https://developer.android.com/training/wearables/notifications/voice-input.html
    public static final String LOCATION_VOICE_REPLY = "location_voice_reply";
    final static String GROUP_KEY_EMAILS = "group_key_emails";  //to stack notifications as part of a group - https://developer.android.com/training/wearables/notifications/stacks.html
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        //For voice reply
        String replyLabel = getResources().getString(R.string.reply_label);
        String[] replyChoices = getResources().getStringArray(R.array.reply_choices);

        RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                .setLabel(replyLabel)
                .setChoices(replyChoices)
                .build();

        // Create an intent for the reply action
        Intent replyIntent = new Intent(this, ReplyActivity.class);
        PendingIntent replyPendingIntent =
                PendingIntent.getActivity(this, 0, replyIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

        // Create the reply action and add the remote input
        NotificationCompat.Action action =
                new NotificationCompat.Action.Builder(R.drawable.bill_pay,
                        getString(R.string.label), replyPendingIntent)
                        .addRemoteInput(remoteInput)
                        .build();

        //This is the First notification sent from mobile to wear
        //Build the notification and add the action via WearableExtender
        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_message)
                .setContentTitle(getString(R.string.title))
                .setContentText(getString(R.string.content))
                .extend(new WearableExtender().addAction(action))
                .build();

        //Get an instance of the NotificationManager Service
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        //Build the notification and issues it with notification manager.
        notificationManager.notify(001, notification);

        //  final String reply = getIntent().getStringExtra(MyActivity.EXTRA_VOICE_REPLY);

//        Context context = getApplicationContext();
//        int duration = Toast.LENGTH_SHORT;
//        Toast toast = Toast.makeText(context, EXTRA_VOICE_REPLY, duration);
//        toast.show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {


            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    //Sending normal one page notification
    public void onButtonClick(View view) {
        int notificationId = 002;
        String title = "",
               message = "";
        if(view.getId() == R.id.button1) {
            title = "Hello Joseph";
            message = "Welcome to Yodlee PFM for Wear.\nSwipe Left for Details";
        } else {
            title = "Notification2";
            message = "Message for Notification 2";
        }
        NotificationCompat.WearableExtender wearableExtender =
                new NotificationCompat.WearableExtender()
                        .setHintShowBackgroundOnly(true);

        Notification notification =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(title)
                        .setContentText(message)
                        .extend(wearableExtender)
                        .setGroup(GROUP_KEY_EMAILS) //for grouping and stacking
                        .build();
        NotificationManagerCompat notificationManager =
                NotificationManagerCompat.from(this);

        notificationManager.notify(notificationId, notification); //use dynamic ids
    }

    //sending large text notification
    public void onButtonClick1(View view) {
        //Add a Big View
        //sends a big style notification to wear. Event description is longer
        //https://developer.android.com/training/wearables/notifications/creating.html
        String eventDescription = "You can insert extended text content to your notification by adding one of the \"big view\" styles to your notification. On a handheld device, users can see the big view content by expanding the notification. On a wearable device, the big view content is visible by default.\n" +
                "\n";
        NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
        bigStyle.bigText(eventDescription);

        NotificationCompat.WearableExtender wearableExtender =
                new NotificationCompat.WearableExtender()
                        .setHintShowBackgroundOnly(false);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_message)
                        .setLargeIcon(BitmapFactory.decodeResource(
                                getResources(), R.drawable.bill_pay))
                        .setContentTitle("Long Notification")
                        .setContentText("Long")
                        .extend(wearableExtender)
                        .setStyle(bigStyle)
                        .setGroup(GROUP_KEY_EMAILS); //for grouping and stacking;

        NotificationManagerCompat notificationManager =
                NotificationManagerCompat.from(this);

        notificationManager.notify(003, notificationBuilder.build()); //use dynamic ids

    }

    //location based notifications
    public void onButtonClick2(View view) {
        //For voice reply
        String replyLabel = getResources().getString(R.string.reply_label);
        String[] replyChoices = getResources().getStringArray(R.array.reply_choices);

        RemoteInput remoteInput1 = new RemoteInput.Builder(LOCATION_VOICE_REPLY)
                .setLabel(replyLabel)
                .setChoices(replyChoices)
                .build();

        // Create an intent for the reply action
        Intent replyIntent1 = new Intent(this, LocationReplyActivity.class);
        PendingIntent replyPendingIntent1 =
                PendingIntent.getActivity(this, 0, replyIntent1,
                        PendingIntent.FLAG_UPDATE_CURRENT);

        // Create the reply action and add the remote input
        NotificationCompat.Action action1 =
                new NotificationCompat.Action.Builder(R.drawable.bill_pay,
                        getString(R.string.locationLabel), replyPendingIntent1)
                        .addRemoteInput(remoteInput1)
                        .build();

        //This is the First notification sent from mobile to wear
        //Build the notification and add the action via WearableExtender
        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.bill_pay)
                .setContentTitle("")
                .setContentText("Do you want to enable location based notifications?")
                .extend(new WearableExtender().addAction(action1))
                .build();

        //Get an instance of the NotificationManager Service
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        //Build the notification and issues it with notification manager.
        notificationManager.notify(006, notification);
    }
}

